package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.mapper.YddAdvposMapper;
import com.itheima.ydd.pojo.YddAdvimg;
import com.itheima.ydd.pojo.YddAdvpos;
import com.itheima.ydd.pojo.YddArticle;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements YddAdvposService {

    @Autowired
    private YddAdvimgService yddAdvimgService;

    //广告位修改
    @Override
    public ResponseEntity<R<String>> updateYddAdvops(YddAdvpos yddAdvpos) {
        updateById(yddAdvpos);
        return ResponseEntity.ok(R.success("修改成功"));
    }

    //广告位删除
    @Override
    public ResponseEntity<R<String>> delYddAdvops(Integer id) {

        LambdaQueryWrapper<YddAdvimg> advimgQueryWrapper = new LambdaQueryWrapper<>();
        advimgQueryWrapper.eq(YddAdvimg::getAdvimgpos, id);
        List<YddAdvimg> advimgList =yddAdvimgService.list(advimgQueryWrapper);

        AdvposDto advposDto = new AdvposDto();
        advposDto.setAdvposnum(advimgList.size());

        if(advposDto.getAdvposnum()!=0) return ResponseEntity.ok(R.err("删除失败,有关联数据不能删除"));
        removeById(id);
        return ResponseEntity.ok(R.success("删除成功"));
    }

    //广告位添加
    @Override
    public ResponseEntity<R<String>> addYddAdvops(YddAdvpos yddAdvpos) {
        save(yddAdvpos);
        return ResponseEntity.ok(R.success("添加成功"));
    }

    /**
     * 分页
     * @param page 页码,
     * @param limit 每页数量
     * @param advposdesc  广告位描述
     * @param advposname 广告位名称,支持模糊查询
     * @return
     */
    @Override
    public ResponseEntity<R<PageInfo<AdvposDto>>> findPage(Integer page, Integer limit, String advposdesc, String advposname) {
        IPage<YddAdvpos> pg=new Page<YddAdvpos>(page,limit);
        LambdaQueryWrapper<YddAdvpos> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(advposdesc),YddAdvpos::getAdvposdesc,advposdesc);
        wrapper.like(StringUtils.hasText(advposname),YddAdvpos::getAdvposname,advposname);
        wrapper.orderByAsc(YddAdvpos::getId);
        page(pg,wrapper);

        Page<AdvposDto> advposDtoPage = new Page<>(page, limit);

        List<AdvposDto> AdvposDtos = pg.getRecords().stream().map((advpos) -> {

            Integer id = advpos.getId();
            LambdaQueryWrapper<YddAdvimg> advimgQueryWrapper = new LambdaQueryWrapper<>();
            advimgQueryWrapper.eq(YddAdvimg::getAdvimgpos, id);
            List<YddAdvimg> advimgList =yddAdvimgService.list(advimgQueryWrapper);

            AdvposDto advposDto = new AdvposDto();
            BeanUtils.copyProperties(advpos, advposDto);

            advposDto.setAdvposnum(advimgList.size());

            return advposDto;
        }).collect(Collectors.toList());
        advposDtoPage.setRecords(AdvposDtos);
        BeanUtils.copyProperties(pg, advposDtoPage, "records");


        PageInfo<AdvposDto> pageInfo = new PageInfo<>();
        //将数据封装到pageinfo新对象中
        pageInfo.setCount((int) advposDtoPage.getTotal());
        pageInfo.setTotalPages((int) advposDtoPage.getPages());
        pageInfo.setPageSize((int) advposDtoPage.getSize());
        pageInfo.setCurrentPage((int) advposDtoPage.getCurrent());
        pageInfo.setData(advposDtoPage.getRecords());
        return ResponseEntity.ok(R.success(pageInfo));
    }

    /**
     * 查询全部数据
     * @return
     */
    @Override
    public ResponseEntity<R<List<YddAdvpos>>> findAllData() {
        List<YddAdvpos> list = list();
        return ResponseEntity.ok(R.success(list));
    }
}
